package com.jxb.tree;

/**
 * 平衡二叉树-一个二叉树每个节点的左右两个子树的高度差的绝对值不超过 1
 */
public class BalancedBinTree_110 {

    public static void main(String[] args) {

    }

    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        return depth(root) != -1;
    }

    private int depth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = depth(root.left);
        int right = depth(root.right);
        if (left == -1 || right == -1 || Math.abs(left - right) > 1) {
            return -1;
        }
        return Math.max(left,right) + 1;
    }

}
